package org.bwgl;

import java.lang.annotation.Annotation;

@MyTypeAnnotation(value = "后来的注解")
public class Main {
    @MyFieldAnnotation
    int money;

    @MyMethodAnnotation
    String hello(@MyParameter1 @MyParameter2 String name) {
        return "hello, " + name;
    }

    public static void main(String[] args) {
        MyTypeAnnotation annotation = Main.class.getAnnotation(MyTypeAnnotation.class);
        System.out.println(annotation.value());
        try {
            MyFieldAnnotation money = Main.class.getDeclaredField("money").getAnnotation(MyFieldAnnotation.class);
            MyMethodAnnotation hello = Main.class.getDeclaredMethod("hello", String.class).getAnnotation(MyMethodAnnotation.class);
            Annotation[][] hellos = Main.class.getDeclaredMethod("hello", String.class).getParameterAnnotations();

            System.out.println(money.value());
            System.out.println(hello.value());

            for (Annotation[] aa :
                    hellos) {
                for (Annotation a :
                        aa) {
                    if (a instanceof MyParameter1) {
                        System.out.println(((MyParameter1) a).value());
                    }
                    if (a instanceof MyParameter2) {
                        System.out.println(((MyParameter2) a).value());
                    }
                }
            }
        } catch (NoSuchFieldException | NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}
